#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2016-06-04 02:29:11
# Project: Wnacg

from pyspider.libs.base_handler import *
import re

base = "http://www.wnacg.org"
numexp = re.compile('\d+')
http_url = re.compile('(http://[^"]+)\\\\')


def num(source):
    return int(numexp.findall(source)[0])


class Handler(BaseHandler):
    crawl_config = {
        'itag': 'v4',
        'headers': {
            "User-Agent": "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C28 Safari/419.3"
        }
    }

    @every(minutes=24*60)
    def on_start(self):
        self.crawl('%s/albums.html' % base, callback=self.index_page, age=24 * 60 * 60, auto_recrawl=True, force_update=True)

    @config()
    def index_page(self, response):
        counts = max(map(lambda x: int(x.text()), response.doc('.paginator>a[href^="http"]').items()))
        for index in xrange(1, counts + 1):
            self.crawl("%s/albums-index-page-%d.html" % (base, index), callback=self.albums_page, age=1 * 60 * 60, auto_recrawl=True, force_update=True)

    @config(priority=2)
    def albums_page(self, response):
        result = []
        for each in response.doc('.con_mod a').items():
            aid = num(each.attr.href)
            self.crawl('http://m.wnacg.net/photos-gallery-aid-%d.html' % aid, callback=self.slide_page)
            title = each.find('.title_name').text().encode('utf-8')
            pages = num(each.find('.col_99')[0].text)
            cover = each.find('img').attr.src
            result.append({"aid": aid, "title": title, "pages": pages, "cover": cover})
            # return result

    @config(priority=99, age=100 * 24 * 60 * 60)
    def slide_page(self, response):
        urls = []
        for url in http_url.findall(response.text.encode('utf-8')):
            if url.endswith('shoucang.jpg'):
                continue
            url = url.replace("/m/", "/data/")
            urls.append(url)
        return urls
